<script lang="ts">
export default {
  name: "admin_openai"
}
</script>
<template>
  <div>
    <t-space direction="vertical" style="width: 100%">
      <t-input size="large" placeholder="请输入搜索内容，响应时间会有点长，请耐心等待，一般 30 秒左右。"
               v-model="OpenAI.Keyword"/>
      <t-button size="large" theme="primary" @click="OnSearch"
                :disabled="OpenAI.Keyword.length <= 2 || OpenAI.Disabled">
        <template #icon>
          <SearchIcon/>
        </template>
        确认搜索
      </t-button>
      <div v-html="OpenAI.Content"></div>
    </t-space>
  </div>
</template>
<script lang="ts" setup>
import {SearchIcon} from "tdesign-icons-vue-next";
import {reactive} from "vue";
import {MessagePlugin} from "tdesign-vue-next";
import {marked} from "marked";
import api from "../../../api/api";

const OpenAI = reactive({
  Keyword: "",
  Disabled: false,
  Content: ""
})
const OnSearch = async () => {
  OpenAI.Disabled = true
  OpenAI.Content = "正在执行搜索中，请稍后，一般 30 秒左右。"
  let ld = MessagePlugin.loading("正在执行搜索中，请稍后，一般 30 秒左右。", 0)
  let res = await api.Admin.OpenAI.Search({
    keyword: OpenAI.Keyword,
  })
  MessagePlugin.close(ld)
  OpenAI.Disabled = false
  if (res != null) {
    OpenAI.Content = res.data
    OpenAI.Content = marked(OpenAI.Content)
    OpenAI.Content = OpenAI.Content.replace(/<pre>/g, '<pre class="line-numbers">')
    OpenAI.Content = OpenAI.Content.replace(/<a href/g, '<a target="_blank" href')
    setTimeout(() => {
      // @ts-ignore
      Prism.highlightAll()
    }, 200)
  }
}
</script>